package com.tbit.uqbike.object.pojo.dto;

import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.pojo.PowerChangeMonitorConfig;
import com.tbit.uqbike.object.view.BasePageQuery;
import com.tbit.uqbike.webmanager.util.Assert;
import com.tbit.uqbike.webmanager.util.StringUtil;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.StringUtils;

import java.util.*;
import java.util.function.IntFunction;

/**
 * @author yee_
 * @version 1.0
 * @date 2023/5/4 10:18
 */
@Getter
@Setter
@Accessors(chain = true)
@ToString
public class WorkOrderLogQueryDTO extends BasePageQuery {
    private static final String BETWEEN_TEMPLATE = " BETWEEN %d AND %d";
    private Integer accountId;
    private String userCode;
    private Integer accountUserId;
    private Long startTime;
    private Long endTime;
    private String isValid;
    private Integer machineType;
    private String batteryNONew;
    private String batteryNOOld;
    private String regionIds;
    /**
     * 换电区间
     * 0-馈电  -1~0
     * 1-低电  1%~10%
     * 2-中电 11%~20%
     * 3-高电 21%~100%
     */
    private Integer powerRange;
    private String powerRangeSqlConcat;
    private Integer idleRange;
    private String idleRangeSqlConcat;
    private Integer auditType;
    private Integer auditorId;

    /**
     * 调度归属星期
     */
    private Integer dispatchVestingWeek;

    /**
     * 调度归属时间段
     */
    private Integer dispatchVestingTimePeriod;

    public void selfCheck() {
        Assert.notNull(accountId, "ver.accountId.notNull");
        calculatePageNumbers();
    }

    public Date getStartTime() {
        return Optional.ofNullable(this.startTime).map(Date::new).orElse(null);
    }

    public Date getEndTime() {
        return Optional.ofNullable(this.endTime).map(Date::new).orElse(null);
    }

    public List<Integer> getIsValid() {
        return Optional.ofNullable(this.isValid).filter(StringUtils::isNotBlank)
                .map(StringUtil::getIntegerList)
                .orElse(Collections.emptyList());
    }

    public List<Integer> getRegionIds() {
        return Optional.ofNullable(this.regionIds).filter(StringUtils::isNotBlank)
                .map(StringUtil::getIntegerList)
                .orElse(Collections.emptyList());
    }

    public void buildPowerRangeSqlCondition(IntFunction<PowerChangeMonitorConfig> selectConfigFn) {
        if (Objects.isNull(this.powerRange)) {
            return;
        }

        PowerChangeMonitorConfig monitorConfig = selectConfig(selectConfigFn);
        switch (this.powerRange) {
            case 0:
                this.powerRangeSqlConcat = String.format(" < %d", monitorConfig.getPowerLowMin());
                return;
            case 1:
                this.powerRangeSqlConcat = String.format(BETWEEN_TEMPLATE, monitorConfig.getPowerLowMin(), monitorConfig.getPowerLowMax());
                return;
            case 2:
                this.powerRangeSqlConcat = String.format(BETWEEN_TEMPLATE, monitorConfig.getPowerMiddleMin(), monitorConfig.getPowerMiddleMax());
                return;
            case 3:
                this.powerRangeSqlConcat = String.format(" >= %d", monitorConfig.getPowerHighMin());
                return;
            default:
                throw new BaseException("未匹配到换电区间设置");
        }
    }

    public void buildDispatchSqlCondition(IntFunction<PowerChangeMonitorConfig> selectConfigFn) {
        if (Objects.isNull(this.idleRange)) {
            return;
        }
        PowerChangeMonitorConfig monitorConfig = selectConfig(selectConfigFn);
        switch (this.idleRange) {
            case 0:
                this.idleRangeSqlConcat = String.format(BETWEEN_TEMPLATE, monitorConfig.getIdleShortMin(), monitorConfig.getIdleShortMax());
                return;
            case 1:
                this.idleRangeSqlConcat = String.format(BETWEEN_TEMPLATE, monitorConfig.getIdleMiddleMin(), monitorConfig.getIdleMiddleMax());
                return;
            case 2:
                this.idleRangeSqlConcat = String.format(" >= %d", monitorConfig.getIdleLongMin());
                return;
            default:
                throw new BaseException("未匹配到闲置区间设置");
        }
    }

    private PowerChangeMonitorConfig selectConfig(IntFunction<PowerChangeMonitorConfig> selectConfigFn) {
        return Optional.ofNullable(selectConfigFn.apply(this.accountId))
                .orElseGet(() -> new PowerChangeMonitorConfig().putDefaultValue(this.accountId));
    }

}
